const express = require('express')
const comment = express.Router()

// 引入数据库
const db = require('../utils/db')

/* 获取店铺对应的评价信息 */
comment.get('/getcommentsbyid', (req, res) => {
   const { id } = req.query
   const sql = `SELECT * FROM comments WHERE shoplist_id=${id}`
   db.query(sql, (err, results) => {
      if (results.length >= 1) res.json({ comments: results, msg: 'ok' })
      else res.json({ comments: [], msg: 'no' })
   })
})

/* 新增一条评论 */
comment.post('/indertcomment', (req, res) => {
   const { comments_user, content, shoplist_id, userpic, user_id, score } = req.body
   const sql = `INSERT INTO comments(comments_user,content,shoplist_id,userpic,user_id,score) VALUES('${comments_user}','${content}',${shoplist_id},'${userpic}',${user_id},${score})`
   db.query(sql, (err, results) => {
      if (results.affectedRows >= 1) {
         res.json({ code: 1, msg: 'ok' })
      } else res.json({ code: 0, msg: 'failed' })
   })
})

module.exports = comment